<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%cms_channel}}".
 *
 * @property int $id
 * @property int $parent_id 父级频道ID
 * @property string $name 名称
 * @property string $abbr 缩写
 * @property string|null $thumb 缩略图
 * @property string|null $description 描述
 * @property string|null $template 自定义显示模版
 * @property string|null $link 跳转链接
 * @property int $listorder 排序
 * @property int $recommend_at 推荐时间
 * @property int $reserved 是否保留类型
 * @property int $navbar 是否导航
 * @property string|null $scope 应用范围
 * @property int $scope_id 应用范围ID
 * @property int $map_id MAPID
 * @property string|null $map_data MAP参数
 * @property int $status 状态
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class CmsChannel extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    //
    const SCOPE_HOME = 'home';
    const SCOPE_SPECIAL = 'special';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_channel}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['parent_id', 'listorder', 'recommend_at', 'reserved', 'navbar', 'scope_id', 'map_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'abbr'], 'required'],
            [['name', 'abbr', 'scope'], 'string', 'max' => 50],
            [['thumb', 'description', 'link', 'map_data'], 'string', 'max' => 255],
            [['template'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'parent_id' => '父级频道ID',
            'name' => '名称',
            'abbr' => '缩写',
            'thumb' => '缩略图',
            'description' => '描述',
            'template' => '自定义显示模版',
            'link' => '跳转链接',
            'listorder' => '排序',
            'recommend_at' => '推荐时间',
            'reserved' => '是否保留类型',
            'navbar' => '是否导航',
            'scope' => '应用范围',
            'scope_id' => '应用范围ID',
            'map_id' => 'MAPID',
            'map_data' => 'MAP参数',
            'status' => '状态',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public static function findActived($condtion = []) {
        return static::find()->active(static::STATUS_ACTIVE)->andFilterWhere($condtion)->orderBy([
            'listorder' => SORT_ASC
        ])->all();
    }

    /**
     * {@inheritdoc}
     */
    public function getScopeList() {
        return [
            'special' => '专题'
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getIcon() {
        $thumb = '/assets/icons/channgel.png';
        if ($this->thumb) {
            $thumb = $this->thumb;
        }
        return Url::attach($thumb);
    }

    /**
     * {@inheritdoc}
     */
    public function getBadge() {
        return $this->recommend_at ? true : false;
    }

    /**
     * {@inheritdoc}
     */
    public function getMplink() {
        $link = $this->link;
        if (!$link) {
            $link = '/pages/channel/main';
        }
        return Url::mplink($link, [
            'id' => $this->id,
            'title' => $this->name
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'name',
            'abbr',
            'icon',
            'template',
            'mplink'
        ];
    }

}
